﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using PowerCreator.LMS.ViewQuery;
using System.Text;
using PowerCreator.LMS.Common.Script;

public partial class sysadmin_PaperManager_ProjectUserPaperList : PowerCreator.LMS.Web.Base.WebBasePage
{

    ViewQuery objViewQuery = new ViewQuery();
    const string SEARCH_PROJECTUSERPAPERList = "select UPHistoryID,FKUPHistoryUserID,UPHistoryReadState,UPHistoryScore,UPHistorySubmitTime,UPHistoryReadTime,TUser.UserName from UserPaperHistory,TUser where FKUPHistoryUserID=UserID "
        +"and FKUPHistoryProjectID={0} and FKUPHistoryCourseID={1} and FKUPHistoryClassID={2} and FKUPHistoryPaperID={3}";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            ViewState["PaperID"] = Request.QueryString["PaperID"].ToString();
            ViewState["ProjectID"] = Request.QueryString["ProjectID"].ToString();
            ViewState["ClassID"] = Request.QueryString["ClassID"].ToString();
            ViewState["CourseID"] = Request.QueryString["CourseID"].ToString();   
            ViewState["PaperID"] = Request.QueryString["PaperID"].ToString();
            ViewState["ProjectResourceID"] = Request.QueryString["ProjectResourceID"].ToString();
            ViewState["CourseResourceID"] = Request.QueryString["CourseResourceID"].ToString();
            ViewState["PaperName"] = Request.QueryString["PaperName"].ToString();
            ViewState["TrainclaName"] = Request.QueryString["TrainclaName"].ToString();

            string search = GetSearch();
            BinderPager(search);
            BinderRPT(search, 1);
        }
    }
    private string GetSearch()
    {
        StringBuilder search = new StringBuilder();
        search.Append(string.Format(SEARCH_PROJECTUSERPAPERList, ViewState["ProjectID"].ToString(), ViewState["CourseID"].ToString(), ViewState["ClassID"].ToString(), ViewState["PaperID"].ToString()));
        if (txt_UserName.Text.Trim() != "")
        {
            search.Append(" and UserName  like '%" + txt_UserName.Text.Trim() + "%'");
        }
        return search.ToString();
    }

    private void BinderPager(string search)
    {
        AspNetPager1.PageSize = LMSPageSize;
        AspNetPager1.RecordCount = objViewQuery.Count(search);
    }

    private void BinderRPT(string search, int pageIndex)
    {
        DataTable dt = objViewQuery.Query(search, LMSPageSize, pageIndex);
        rpt_ProjectList.DataSource = dt;
        dt.Columns.Add("url");
        dt.Columns.Add("StateName");
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            dt.Rows[i]["url"] = dt.Rows[i]["Url"] = "PaperID=" + ViewState["PaperID"].ToString() + "&ProjectID=" + ViewState["ProjectID"].ToString() + "&ClassID=" + ViewState["ClassID"].ToString() + "&CourseID=" + ViewState["CourseID"].ToString() + "&ProjectResourceID=" + ViewState["ProjectResourceID"].ToString() + "&CourseResourceID=" + ViewState["CourseResourceID"].ToString() + "&UserID=" + dt.Rows[i]["FKUPHistoryUserID"].ToString();
            if (dt.Rows[i]["UPHistoryReadState"].ToString().Trim() == "1")
            {
                dt.Rows[i]["StateName"] = GetLanageValueByKey("msg_PaperUserList_yipiyue", PowerCreator.LMS.Language.LanagetItemType.Page);
            }
            else
            {
                dt.Rows[i]["StateName"] = GetLanageValueByKey("msg_PaperUserList_weipiyue", PowerCreator.LMS.Language.LanagetItemType.Page);
            }
        }


        rpt_ProjectList.DataBind();
        for (int i = 0; i < rpt_ProjectList.Items.Count; i++)
        {
            Label lab_ReadState = (Label)rpt_ProjectList.Items[i].FindControl("lab_ReadState");
            LinkButton btn_see = (LinkButton)rpt_ProjectList.Items[i].FindControl("btn_see");
            LinkButton btn_read = (LinkButton)rpt_ProjectList.Items[i].FindControl("btn_read");
            LinkButton btn_edit = (LinkButton)rpt_ProjectList.Items[i].FindControl("btn_edit");

            if (lab_ReadState.Text.Trim() == "1")
            {
                btn_see.Enabled = true;
                btn_read.Enabled = false;
                btn_edit.Enabled = true;
            }
            else
            {
                btn_see.Enabled = false;
                btn_read.Enabled = true;
                btn_edit.Enabled = false;

            }
        }
    }
    protected void btn_Search_Click(object sender, EventArgs e)
    {
        //ViewState["CourseID"] = "0";
        string search = GetSearch();
        BinderPager(search);
        BinderRPT(search, 1);
    }
    protected void rpt_ProjectList_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        //LinkButton btn_see=(LinkButton)e.Item.FindControl("btn_see");
        //LinkButton btn_read = (LinkButton)e.Item.FindControl("btn_read");
        //LinkButton btn_edit = (LinkButton)e.Item.FindControl("btn_edit");
        
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        string search = GetSearch();
        BinderPager(search);
        BinderRPT(search, 1);
    }
    protected void rpt_ProjectList_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.CommandName == "btn_see")
        {
            Script.NewOpenURL("ReadedPaper.aspx?"+e.CommandArgument.ToString());
        }
        if (e.CommandName == "btn_read")
        {
            Script.NewOpenURL("ReadPaper.aspx?" + e.CommandArgument.ToString());
        }
        if (e.CommandName == "btn_edit")
        {
            Script.NewOpenURL("EditUserPaper.aspx?" + e.CommandArgument.ToString());
        }
    }
    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        string search = GetSearch();
        BinderRPT(search, AspNetPager1.CurrentPageIndex);
    }
}
